// npm i -S redux redux-thunk react-redux
// redux的入口文件
// combineReducers 它可以实现多个reducer的合并，多个reducer合并成一个reducer
// redux中的合并，它只对 state有命名空间，reducer中的操作没有命名空间
// 所以action中的操作名称不能重复，如果重复，则会执行多次
import { createStore, applyMiddleware , combineReducers } from 'redux'
// import { combineReducers } from 'redux-immutable'
// 异步中间件
import thunk from 'redux-thunk'
// 调试
import { composeWithDevTools } from 'redux-devtools-extension'

// 自动化引入 
let moduleFn = require.context('./reducers', false, /\.js$/)
let modules = moduleFn.keys().reduce((p, c) => {
  let mod = moduleFn(c).default;
  let modName = c.match(/\.\/(\w+)\.js$/)[1]
  p[modName] = mod
  return p
}, {})


// 合并多个reducer
let reducer = combineReducers(modules)

export default createStore(
  reducer,
  process.env.NODE_ENV === 'development'
    ?
    composeWithDevTools(applyMiddleware(thunk))
    :
    applyMiddleware(thunk)
)